package com.comtop.springboot.templet.modules.job.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.comtop.springboot.templet.common.annotation.SysLog;
import com.comtop.springboot.templet.common.utils.QueryMap;
import com.comtop.springboot.templet.common.utils.Ret;
import com.comtop.springboot.templet.common.validator.ValidatorUtils;
import com.comtop.springboot.templet.modules.job.service.IScheduleJobService;
import com.comtop.springboot.templet.modules.job.entity.ScheduleJobEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Map;

/**
 * 定时任务
 * 
 * @author wangjiafang
 * @version 1.0
 * @since 2016年11月28日 下午2:16:40
 */
@RestController
@RequestMapping("/sys/schedule")
public class ScheduleJobController {
	@Autowired
	private IScheduleJobService IScheduleJobService;
	
	/**
	 * 定时任务列表
	 */
	@GetMapping("/list")
	@RequiresPermissions("sys:schedule:list")
	public Ret list(@RequestParam Map<String, Object> params){
		//查询列表数据
		QueryMap queryMap = new QueryMap(params);
		Page<ScheduleJobEntity> scheduleJobEntityPage = IScheduleJobService.queryScheduleJobList(new Page<ScheduleJobEntity>(queryMap.getCurrent(), queryMap.getSize()), params);
		return Ret.ok().put("page", scheduleJobEntityPage);
	}
	
	/**
	 * 定时任务信息
	 */
	@GetMapping("/info/{jobId}")
	@RequiresPermissions("sys:schedule:info")
	public Ret info(@PathVariable("jobId") Long jobId){
		ScheduleJobEntity schedule = IScheduleJobService.selectById(jobId);
		
		return Ret.ok().put("schedule", schedule);
	}
	
	/**
	 * 保存定时任务
	 */
	@SysLog("保存定时任务")
	@PostMapping("/save")
	@RequiresPermissions("sys:schedule:save")
	public Ret save(@RequestBody ScheduleJobEntity scheduleJob){
		ValidatorUtils.validateEntity(scheduleJob);
		
		IScheduleJobService.save(scheduleJob);
		
		return Ret.ok();
	}
	
	/**
	 * 修改定时任务
	 */
	@SysLog("修改定时任务")
	@PostMapping("/update")
	@RequiresPermissions("sys:schedule:update")
	public Ret update(@RequestBody ScheduleJobEntity scheduleJob){
		ValidatorUtils.validateEntity(scheduleJob);
				
		IScheduleJobService.update(scheduleJob);
		
		return Ret.ok();
	}
	
	/**
	 * 删除定时任务
	 */
	@SysLog("删除定时任务")
	@PostMapping("/delete")
	@RequiresPermissions("sys:schedule:delete")
	public Ret delete(@RequestBody Long[] jobIds){
		IScheduleJobService.deleteBatch(jobIds);
		
		return Ret.ok();
	}
	
	/**
	 * 立即执行任务
	 */
	@SysLog("立即执行任务")
	@PostMapping("/run")
	@RequiresPermissions("sys:schedule:run")
	public Ret run(@RequestBody Long[] jobIds){
		IScheduleJobService.run(jobIds);
		
		return Ret.ok();
	}
	
	/**
	 * 暂停定时任务
	 */
	@SysLog("暂停定时任务")
	@PostMapping("/pause")
	@RequiresPermissions("sys:schedule:pause")
	public Ret pause(@RequestBody Long[] jobIds){
		IScheduleJobService.pause(jobIds);
		
		return Ret.ok();
	}
	
	/**
	 * 恢复定时任务
	 */
	@SysLog("恢复定时任务")
	@PostMapping("/resume")
	@RequiresPermissions("sys:schedule:resume")
	public Ret resume(@RequestBody Long[] jobIds){
		IScheduleJobService.resume(jobIds);
		
		return Ret.ok();
	}

}
